package cesaredu.midchat.middlewaremetrics;

import cesaredu.midchat.middleware.MessageSentListener;
import cesaredu.midchat.middleware.MidChatCommunication;

public class MiddlewareMetrics {
	
	private MidChatCommunication middleware;
	private long initialTime;
	
	private long somaTemposMedios;
	private int quantidadeMedias;
	
	public MiddlewareMetrics(MidChatCommunication middleware) {
		this.middleware = middleware;
	}
	
	public void startMeasure() {
		
		MessageSentListener messageSentListener = new MessageSentListener() {

			@Override
			public void onMessageSent(MessageSentResult result) {
				long finalTime = System.nanoTime();
				long elapsedTime = (finalTime - MiddlewareMetrics.this.initialTime) / 1000000;
				
				MiddlewareMetrics.this.somaTemposMedios += elapsedTime;
				MiddlewareMetrics.this.quantidadeMedias++;
				
				System.out.println("Concluido " + elapsedTime);
			}
		};
		
		this.initialTime = System.nanoTime();
		this.middleware.sendMessage("1234567890", "Teste", messageSentListener);
	}
	
	public long getMedia() {
		return this.somaTemposMedios / this.quantidadeMedias;
	}
}